package org.popupcommunication.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.popupcommunication.exceptions.SujetNotFoundException;
import org.popupcommunication.model.Group;
import org.popupcommunication.model.ResponseForum;
import org.popupcommunication.model.SujetForum;
import org.popupcommunication.model.User;
import org.popupcommunication.service.GroupManager;
import org.popupcommunication.service.ResponseForumManager;
import org.popupcommunication.service.SujetForumManager;
import org.popupcommunication.service.UserManager;
import org.popupcommunication.utils.BrowserFiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("fichiers")
public class FilesController {
	@Autowired
	UserManager userService;
	@Autowired
	GroupManager groupService;
	@Autowired
	SujetForumManager sujetForumService;
	@Autowired
	ResponseForumManager reponseForumService;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String viewListForum(HttpSession session, Model model) throws UnsupportedEncodingException {
		String path = "";
		User currentUser = null;
		if (session.getAttribute("userLogged") == null) {
			Authentication auth = SecurityContextHolder.getContext()
					.getAuthentication();
			String name = auth.getName();
			currentUser = userService.findByEmail(name);
			session.setAttribute("userLogged", currentUser);
		} else {
			currentUser = (User) session.getAttribute("userLogged");
		}
		// plusieurs groupes
		if (currentUser.getNbGroup() > 1) {
			model.addAttribute("utilisateur", currentUser);
			path = "fichiers/list";
		} else {
			if (currentUser.onlyOneGroup()) {
				Group g = currentUser.getSingleGroup();
				path = "redirect:/fichiers/" + g.getSlug();
			}
		}
		return path;
	}

	@RequestMapping(value = "/{nameGroupe}", method = RequestMethod.GET)
	public String viewForum(HttpServletRequest request,HttpSession session, Model model,
			@PathVariable String nameGroupe) throws UnsupportedEncodingException {
		User currentUser = null;
		Path rep = null;
		List<String> lst = null;
		
		request.setCharacterEncoding("UTF-8");
		Group g = groupService.findBySlug(nameGroupe);
		session.setAttribute("groupeCurrent", g);
		String repertoire = request.getSession().getServletContext().getRealPath("/resources/upload/group/"+g.getIdGroup());
		rep = Paths.get(repertoire);
		System.out.println("rep= "+rep);
		if (rep == null || !Files.isDirectory(rep))
			return "fichiers/noFile";
		try {
			lst= BrowserFiles.getDirectoryFiles(rep,rep);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("fichiers", lst);

		model.addAttribute("groupe", g);

		return "fichiers/viewFiles";
	}

	
}
